﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class MuridEdit : Form
    {
        private Murid parent = null;
        private OrtuFind ortufind = null;
        public int par_id = -1;
        private string imagePath = "";

        public MuridEdit()
        {
            InitializeComponent();
        }

        public MuridEdit(Murid instance, 
            string stu_id, 
            string stu_name, 
            string stu_alias, 
            string stu_gender, 
            object stu_birthdate, 
            string stu_birthplace, 
            object stu_regdate,
            string stu_regsession,
            string stu_regform,
            string stu_desc,
            string par_id,
            object stu_image)
        {
            InitializeComponent();

            parent = instance;

            this.txtInduk.Text = stu_id;
            this.txtInduk.ReadOnly = true;

            this.txtNama.Text = stu_name;
            this.txtPanggilan.Text = stu_alias;
            
            if (stu_gender == "L")
                this.cmbKelamin.SelectedIndex = 0;
            else if (stu_gender == "P")
                this.cmbKelamin.SelectedIndex = 1;

            this.birthdayBox.Value = (DateTime)stu_birthdate;

            // this.birthdayBox = 
            this.txtTmpLahir.Text = stu_birthplace;
            this.regBox.Text = ((DateTime)stu_regdate).ToShortDateString();// ToString("dd/MM/yyyy");
            this.txtSesi.Text = stu_regsession;
            this.txtAsal.Text = stu_regform;
            this.txtStatus.Text = stu_desc;

            DataTable dTable = Database.getInstance().query("select * from Parent");
            DataRow[] data = dTable.Select("par_id='" + par_id + "'");
            if (data.Length == 1)
            {
                this.txtOrtu.Text = data[0].ItemArray[1].ToString();
                this.txtAlamat.Text = data[0].ItemArray[2].ToString();
                this.txtPekerjaan.Text = data[0].ItemArray[3].ToString();
                this.par_id = int.Parse(par_id);
            }

            if (stu_image != DBNull.Value)
            {
                byte[] result = (byte[])stu_image;
                System.IO.MemoryStream ms = new System.IO.MemoryStream(result);
                this.picFoto.Image = Image.FromStream(ms);
            }

            // Set Maximum stu_id length
            this.txtInduk.MaxLength = 6;
        }

        private void MuridEdit_Load(object sender, EventArgs e)
        {

        }

        private void btnCek_Click(object sender, EventArgs e)
        {
            if (this.txtInduk.Text == "")
            {
                MessageBox.Show("Anda belum memasukkan nomor induk", "Peringatan !");
                return;
            }

            DataTable dt = Database.getInstance().tes("select * from Student where stu_id='" + this.txtInduk.Text + "'");
            if (dt.Rows.Count == 1)
            {
                MessageBox.Show("Nomor induk ini sudah ada", "Peringatan !");
            }
            else
            {
                MessageBox.Show("Nomor induk tidak ditemukan", "Peringatan !");
            }
        }

        private void btnBatal_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOrtu_Click(object sender, EventArgs e)
        {
            if (this.ortufind == null || this.ortufind.IsDisposed)
                ortufind = new OrtuFind(this);

            //check whether the form has been opened or not
            if (((Main)this.MdiParent).isOpenForm(ortufind))
            {
                ortufind = null;
                return;
            }

            ortufind.MdiParent = this.MdiParent;
            ortufind.Show();
        }

        private void btnSimpan_Click(object sender, EventArgs e)
        {
            if (false == checkFieldData())
            {
                MessageBox.Show("Harap isi semua kolom isian !", "Peringatan !");
                return;
            }

            DataTable dt = Database.getInstance().query("select * from Student");

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i].ItemArray[0].ToString() == this.txtInduk.Text)
                {
                    dt.Rows[i]["stu_id"] = this.txtInduk.Text;
                    dt.Rows[i]["stu_name"] = this.txtNama.Text;
                    dt.Rows[i]["stu_alias"] = this.txtPanggilan.Text;

                    if (this.cmbKelamin.Text == "Laki-laki")
                    {
                        dt.Rows[i]["stu_gender"] = "L";
                    }
                    else
                    {
                        dt.Rows[i]["stu_gender"] = "P";
                    }

                    dt.Rows[i]["stu_birthdate"] = this.birthdayBox.Value.Date;
                    dt.Rows[i]["stu_birthplace"] = this.txtTmpLahir.Text;
                    dt.Rows[i]["stu_regdate"] = this.regBox.Text;
                    string[] sesi = this.txtSesi.Text.Split('/');
                    dt.Rows[i]["stu_regsession"] = this.txtSesi.Text;
                    dt.Rows[i]["stu_regform"] = this.txtAsal.Text;
                    dt.Rows[i]["stu_desc"] = this.txtStatus.Text;
                    dt.Rows[i]["par_id"] = this.par_id;
                    dt.Rows[i]["stu_image"] = ImageToByteArray(this.picFoto.Image);         
                    break;
                }
            }
            
            Database.getInstance().updateDB(dt);

            var result = MessageBox.Show("Edit data murid berhasil", "Sukses !");
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                
                int current_row = this.parent.dataGridView1.CurrentCell.RowIndex;

                this.parent.dataGridView1.Rows[current_row].Cells["stu_id"].Value = this.txtInduk.Text;
                this.parent.dataGridView1.Rows[current_row].Cells["stu_regdate"].Value = this.regBox.Text;
                this.parent.dataGridView1.Rows[current_row].Cells["stu_name"].Value = this.txtNama.Text;
                this.parent.dataGridView1.Rows[current_row].Cells["stu_alias"].Value = this.txtPanggilan.Text;
                this.parent.dataGridView1.Rows[current_row].Cells["stu_gender"].Value = this.cmbKelamin.Text;
                this.parent.dataGridView1.Rows[current_row].Cells["stu_birthdate"].Value = this.birthdayBox.Value;
                this.parent.dataGridView1.Rows[current_row].Cells["stu_birthplace"].Value = this.txtTmpLahir.Text;
                this.parent.dataGridView1.Rows[current_row].Cells["stu_regsession"].Value = this.txtSesi.Text;
                this.parent.dataGridView1.Rows[current_row].Cells["stu_desc"].Value = this.txtStatus.Text;
                this.parent.dataGridView1.Rows[current_row].Cells["stu_regform"].Value = this.txtAsal.Text;
                this.parent.dataGridView1.Refresh();

                this.Close();
            }
        }

        private Boolean checkFieldData()
        {
            if (this.txtInduk.Text == "" ||
                this.txtNama.Text == "" ||
                this.txtPanggilan.Text == "" ||
                this.cmbKelamin.Text == "" ||
                this.birthdayBox.Text == "" ||
                this.txtTmpLahir.Text == "" ||
                this.regBox.Text == "" ||
                this.txtSesi.Text == "" ||
                this.txtAsal.Text == "" ||
                this.txtStatus.Text == "" ||
                this.txtOrtu.Text == "" ||
                this.txtAlamat.Text == "" ||
                this.txtPekerjaan.Text == "")
            {
                return false;
            }

            return true;
        }

        private byte[] ImageToByteArray(Image img)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }

        private void picFoto_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
            if (open.ShowDialog() == DialogResult.OK)
            {
                // display image in picture box
                this.picFoto.Image = new Bitmap(open.FileName);
                this.imagePath = open.FileName;

            }
        }

        private void txtInduk_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
